package com.proyecto.core.vista.echo.componentes;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import consultas.echo2consultas.SpWindowPane;
import nextapp.echo2.app.ContentPane;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.filetransfer.UploadSelect;
import nextapp.echo2.app.filetransfer.UploadEvent;
import nextapp.echo2.app.filetransfer.UploadListener;
import java.util.TooManyListenersException;

import com.proyecto.core.Utilidades.FileWrapper;

public class WPInterfaces extends SpWindowPane {

	private UploadSelect control;

	/**
	 * Creates a new <code>WPInterfaces</code>.
	 */
	public WPInterfaces() {
		super();
		initComponents();
	}
	
	public String[] procesarLinea(String linea, String tamanoCampos[], String valoresCampos[]){
    	int i=0;
    	int posi=0;
    	while(i < tamanoCampos.length){
           valoresCampos[i]= linea.substring(posi,posi+ Integer.parseInt(tamanoCampos[i]));
           posi=posi+Integer.parseInt(tamanoCampos[i]);
           i++;
    	}
    	return valoresCampos;
	}
	
	public Object procesarDatos(String[] valoresCampos) {
        // Se procesa los particular
    	System.out.println("Datos....");
    	
    	//Este metodo debes sobreescribirlo en la clase hijo, y con el arreglo de valoresCampos que estas recibiendo llenas
    	//los pojos y modificas o insertars en la base de datos segun sea el caso.
    	String[] valores = valoresCampos;
    	
    	return null;
    	
    }
	

	private void actionControl(UploadEvent e) {
		try {
			System.err.println("Probando...");
            // Leer el contenido en bytes del archivo subido.
            byte[] b = new byte[e.getSize()];
            e.getInputStream().read(b, 0, e.getSize());
            
            // Salvar el contenido a disco usando un FileWrapper.
            FileWrapper file = new FileWrapper();
            file.setName(e.getFileName());
            file.setType(e.getContentType());
            file.setContent(b);

            //Tomar cada cada linea del archivo subido en forma de caracteres
            String linea;
            BufferedReader in; 
            File archivo = new File(file.getPath());
            FileInputStream inputStream = new FileInputStream(archivo);
            in = new BufferedReader(new InputStreamReader(inputStream));
            
            String[] tamanoCampos = {"2","8","25","25","25","25","8","1","3","8","4","8","1","1","2","20","1","35","35","35","35","20","15","2","4","12","4","12","2","2","2","2","2","35","1","1","12","1"};
            String[] valoresCampos = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
            
            linea = in.readLine();
            int posi=0;
            while (linea != null) {
                try {
                	procesarLinea(linea,tamanoCampos,valoresCampos);
                    //****
                    //    AQUI VAN LAS COSAS EN PARTICULAR
                    procesarDatos(valoresCampos);
                    linea = in.readLine();

                }
                catch (Exception e1) {
                    System.out.println("Error: " + e1);
                }
                linea = in.readLine();
            }
         // A�adir un bot�n de descarga para el archivo subido
         //   ((UploadSelect) uploadEvent.getSource()).getParent()
         //           .add(getDownloadButton(file));
        } catch (IOException ioe) {
            // Urgs... hacer algo para informar al usuario.
            ioe.printStackTrace();
        }
        System.err.println("Fin de prueba..");
		
	}

	/**
	 * Configures initial state of component.
	 * WARNING: AUTO-GENERATED METHOD.
	 * Contents will be overwritten.
	 */
	private void initComponents() {
		ContentPane contentPane1 = new ContentPane();
		add(contentPane1);
		Column column1 = new Column();
		contentPane1.add(column1);
		Row row1 = new Row();
		column1.add(row1);
		Row row2 = new Row();
		column1.add(row2);
		Row row3 = new Row();
		column1.add(row3);
		control = new UploadSelect();
		try {
			control.addUploadListener(new UploadListener() {
				public void fileUpload(UploadEvent e) {
					actionControl(e);
				}
	
				public void invalidFileUpload(UploadEvent e) {
				}
			});
		} catch (TooManyListenersException ex) {
			throw new RuntimeException(ex.toString());
		}
		row3.add(control);
	}
}
